<template>
  <a href="javascript: void(0);" @click="openLogin">登录</a>
  <teleport to="body">
    <div class="modal" v-if="modalOpen">
      <div class="box">
        <div class="title">
          <span>密码登录</span>
          <span>短信登录</span>
        </div>
        <div class="inp">
          <input type="text" placeholder="用户名 / 手机号码" v-model="state.username">
          <input type="password" placeholder="请输入密码" v-model="state.password">
          <div class="rember">
            <p>
              <input type="checkbox" v-model="state.rember_pass">
              记住密码
            </p>
            <p>忘记密码</p>
          </div>
<!--          <button class="login-btn" @click="modalOpen=false">登&nbsp;录</button>-->
           <button class="login-btn" @click="loginHandle">登&nbsp;录</button>
          <p class="go-login">没有账号
            <Register/>
          </p>
        </div>
      </div>
    </div>
  </teleport>
</template>

<script>
import {ref, reactive, inject} from "vue"
import { ElMessage } from 'element-plus'

import {login} from "@/api/login";
import Register from "@/components/Register";

export default {
  name: "Login",
  components: {Register},
  setup() {
    const modalOpen = ref(false)
    const state = reactive({
      username: "",
      password: "",
      rember_pass: false,
    })

    function openLogin(e) {
      e.preventDefault()
      modalOpen.value = true
    }

    const reload = inject('reload')

    function loginHandle(){
      let username = state.username
      let password = state.password
      login(username, password).then(res=>{
        if(state.rember_pass){
          // 记住token
          sessionStorage.removeItem('user_token')
          sessionStorage.removeItem('user_id')
          sessionStorage.removeItem('user_name')

          localStorage.user_token = res.data.token
          localStorage.user_id = res.data.id
          localStorage.user_name = res.data.username
        }else{
          // 不记住token
          localStorage.removeItem('user_token')
          localStorage.removeItem('user_id')
          localStorage.removeItem('user_name')

          sessionStorage.user_token = res.data.token
          sessionStorage.user_id = res.data.id
          sessionStorage.user_name = res.data.username
        }
        modalOpen.value=false
        reload()
      }).catch(err=>{
        ElMessage.error('账号或密码错误!')
      })
    }
    return {
      modalOpen,
      state,
      openLogin,
      loginHandle
    }
  }
}
</script>

<style scoped>
a:hover {
  color: #ffc210;
}

.modal {
  /* 产生一个遮罩效果 */
  position: absolute;
  left: 0;
  top: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, .5);
  z-index: 2;

  /* 布局内容:水平居中, 垂直居中 */
  display: flex;
  justify-content: center;
  align-items: center;
}

.modal .box {
  /* 产生一个白色背景的矩形框 */
  width: 300px;
  height: 300px;
  background-color: #fff;
  border-radius: 5px;
}

.box .title {
  height: 50px;
  line-height: 50px;
  border-bottom: 1px solid #e6e6e6;

  display: flex;
  justify-content: space-around;

  font-size: 16px;
  font-weight: bold;
  color: #9b9b9b;
}

.box .title span {
  cursor: pointer;
}

.box .inp {
  margin-top: 15px;
  text-align: center;
}

.box .inp input {
  width: 80%;
  height: 30px;
  margin-bottom: 15px;
  padding: 0;

  font-size: 14px;
}

.inp .rember {
  width: 80%;
  margin: 0 auto;
  display: flex;
  justify-content: space-between;

  font-size: 14px;
}

.inp .rember p {
  display: flex;
}

.inp .rember input {
  width: 15px;
  height: 15px;
}

.login-btn {
  width: 80%;
  height: 40px;
  background: #84cc39;
  border:1px solid #84cc39;
  border-radius: 5px;
  font-size: 16px;
  color: #fff;
}

.go-login {
  margin-top: 15px;
  font-size: 14px;
}
</style>